package com.bluetrum.abmate;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;
import android.view.View;

import com.bluetrum.abmate.cmd.SimpleRequestCallback2;
import com.bluetrum.abmate.databinding.ActivityAncSettingsBinding;
import com.bluetrum.abmate.utils.Utils;
import com.bluetrum.abmate.viewmodels.SharedViewModel;
import com.bluetrum.devicemanager.cmd.Command;
import com.bluetrum.devicemanager.cmd.Request;
import com.bluetrum.devicemanager.cmd.request.AncRequest;
import com.bluetrum.devicemanager.cmd.request.DeviceInfoRequest;
import com.google.android.material.slider.Slider;

import dagger.hilt.android.AndroidEntryPoint;
import timber.log.Timber;

@AndroidEntryPoint
public class AncSettingsActivity extends AppCompatActivity {

    private SharedViewModel mViewModel;

    private ActivityAncSettingsBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(SharedViewModel.class);
        binding = ActivityAncSettingsBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        setupSlider();
        setupObserver();

        // 获取最新的ANC和通透模式
        Request request = new DeviceInfoRequest(Command.INFO_ANC_GAIN)
                .requireTransparencyGain();
        mViewModel.sendRequest(request);
    }

    private void setupSlider() {
        binding.ancGainSlider.addOnSliderTouchListener(new Slider.OnSliderTouchListener() {
            @Override
            public void onStartTrackingTouch(@NonNull Slider slider) {

            }

            @Override
            public void onStopTrackingTouch(@NonNull Slider slider) {
                Request request = AncRequest.ncLevelRequest((byte) slider.getValue());
                sendRequestAndWaitSuccess(request, new SimpleRequestCallback2() {
                    @Override
                    public void onComplete(boolean result) {

                    }

                    @Override
                    public void onTimeout() {

                    }
                });
            }
        });
        binding.transparencyGainSlider.addOnSliderTouchListener(new Slider.OnSliderTouchListener() {
            @Override
            public void onStartTrackingTouch(@NonNull Slider slider) {

            }

            @Override
            public void onStopTrackingTouch(@NonNull Slider slider) {
                Request request = AncRequest.transparencyLevel((byte) slider.getValue());
                sendRequestAndWaitSuccess(request, new SimpleRequestCallback2() {
                    @Override
                    public void onComplete(boolean result) {

                    }

                    @Override
                    public void onTimeout() {

                    }
                });
            }
        });
    }

    private void setupObserver() {
        mViewModel.getDeviceAncMode().observe(this, ancMode -> {
            Timber.d("ANC Mode: %s", ancMode);
            if (ancMode != null) {
                switch (ancMode) {
                    case AncRequest.ANC_MODE_OFF:
                        binding.ancGainSlider.setEnabled(false);
                        binding.transparencyGainSlider.setEnabled(false);
                        break;
                    case AncRequest.ANC_MODE_ON:
                        binding.ancGainSlider.setEnabled(true);
                        binding.ancGainLayout.setVisibility(View.VISIBLE);
                        binding.transparencyGainLayout.setVisibility(View.GONE);
                        break;
                    case AncRequest.ANC_MODE_TRANSPARENCY:
                        binding.ancGainLayout.setVisibility(View.GONE);
                        binding.transparencyGainSlider.setEnabled(true);
                        binding.transparencyGainLayout.setVisibility(View.VISIBLE);
                        break;
                }
            }
            // ANC not support
            else {
                binding.ancGainSlider.setEnabled(false);
                binding.transparencyGainSlider.setEnabled(false);
            }
        });

        mViewModel.getDeviceAncGainNum().observe(this, ancGainNum -> {
            if (ancGainNum != null) {
                binding.ancGainSlider.setValueTo(ancGainNum);
                binding.ancGainSlider.setVisibility(View.VISIBLE);
            } else {
                binding.ancGainSlider.setVisibility(View.GONE);
            }
        });

        mViewModel.getDeviceTransparencyGainNum().observe(this, transparencyGainNum -> {
            if (transparencyGainNum != null) {
                binding.transparencyGainSlider.setValueTo(transparencyGainNum);
                binding.transparencyGainSlider.setVisibility(View.VISIBLE);
            } else {
                binding.transparencyGainSlider.setVisibility(View.GONE);
            }
        });

        mViewModel.getDeviceAncGain().observe(this, ancGain -> {
            if (ancGain != null) {
                binding.ancGainSlider.setValue(ancGain);
            }
        });

        mViewModel.getDeviceTransparencyGain().observe(this, transparencyGain -> {
            if (transparencyGain != null) {
                binding.transparencyGainSlider.setValue(transparencyGain);
            }
        });
    }

    /* Request */

    private void startWaitingResponse() {

    }

    private void stopWaitingResponse() {

    }

    /*
    统一处理返回值Boolean的Request，并提示可能的失败和超时
     */
    private void sendRequestAndWaitSuccess(@NonNull final Request request,
                                           @Nullable SimpleRequestCallback2 callback2) {
        // todo: 加上发送等待画面
        startWaitingResponse();

        mViewModel.sendRequest(request, new SimpleRequestCallback2() {
            @Override
            public void onComplete(boolean result) {
                Timber.d("sendRequest result = %s", result);

                if (result) {
                    Utils.showToast(AncSettingsActivity.this, R.string.request_succeeded);
                } else {
                    Utils.showToast(AncSettingsActivity.this, R.string.request_failed);
                }

                // todo: 解除发送等待
                stopWaitingResponse();

                if (callback2 != null) {
                    callback2.onComplete(result);
                }
            }

            @Override
            public void onTimeout() {
                Timber.w("Request timeout");
                Utils.showToast(AncSettingsActivity.this, R.string.request_timeout);

                // todo: 解除发送等待
                stopWaitingResponse();

                if (callback2 != null) {
                    callback2.onTimeout();
                }
            }
        });
    }

}